<%@ page import="com.minxi.jsp.utils.DruidUtil" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>604作业</title>
</head>

<%--
题目一：创建index.jsp页面，并在JSP页面中，通过JDBC查询数据库，将数据库查询结果封装到List<UserInfo>的对象里面，最后将数据通过 out.println() 方法展示在JSP页面上。（数据参考数据格式）
数据格式：
 --%>
<style>
    .nev {
        display: flex;
        justify-content: center;
        align-items: center;
        margin: auto;
        width: 100%;
    }

    .jdbc {
        text-align: center;
        font-size: 20px;
        color: pink;
        display: block;
        border-collapse: collapse;
        border: 1px solid lightcoral;
        background-color: lightcyan;
        line-height: 25px;
        padding: 20px;
    }
</style>
<body>
<%-- UserInfo类 --%>
<%!
    public static class UserInfo {
        private Integer id;
        private String UserName;
        private String UserPhone;

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getUserName() {
            return UserName;
        }

        public void setUserName(String userName) {
            UserName = userName;
        }

        public String getUserPhone() {
            return UserPhone;
        }

        public void setUserPhone(String userPhone) {
            UserPhone = userPhone;
        }

        @Override
        public String toString() {
            return "UserInfo{" +
                    "id=" + id +
                    ",  UserName='" + UserName + '\'' +
                    ",  UserPhone='" + UserPhone + '\'' + '}';
        }
    }
%>

<%!
    public static List<UserInfo> students;
    public static String student = "";

    public static void jdbc() {
        // 定义null值
        PreparedStatement st = null;
        Connection conn = null;
        ResultSet rs = null;
        students = new ArrayList<>();
        try {

            conn = DruidUtil.DruidConnect();
            // 4.定义sql（预编译）
            String sql = "select id,user_name,user_phone from user_info;";

            // 5.获取sql对象
            st = conn.prepareStatement(sql);

            // 6.执行sql语句
            rs = st.executeQuery();
            while (rs.next()) {
                // 把从数据库拿到的数据存起来
                int id = rs.getInt("id");
                String name = rs.getString("user_name");
                String phone = rs.getString("user_phone");

                // 用拿到的数据创建用户对象
                UserInfo userInfo = new UserInfo();
                userInfo.setId(id);
                userInfo.setUserName(name);
                userInfo.setUserPhone(phone);

                // 往学生列表里添加用户对象
                students.add(userInfo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                DruidUtil.DruidClose(rs, st, conn);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
%>
<div class="nev">
    <p class="jdbc">
        index的内容（JDBC查询数据库）：
        <br><br>
        <%
            jdbc();
            request.setCharacterEncoding("UTF-8");
            // 判断学生列表是否为空并打印
            if (!students.isEmpty()) {
                out.print("[<br>");
                student += "[<br>";
                for (int i = 0; i < students.size(); i++) {
                    String userInfo = String.valueOf(students.get(i));
                    if (i != students.size() - 1) {
                        out.print(students.get(i) + ",<br>");
                        student += userInfo + ",<br>";
                    } else {
                        out.print(students.get(i));
                        student += userInfo;
                    }
                }
                out.print("<br>]");
                student += "<br>]";
            } else {
                out.print("抱歉，没有查询到学生数据~");
                student += "抱歉，没有查询到学生数据~";
            }
        %>
    </p>
</div>
<jsp:include page="index1.jsp">
    <jsp:param name="student" value="<%=student%>"/>
</jsp:include>
</body>
</html>
